#include "shm.h"
#include <unistd.h>

int main(){
  int shmid = shmget(SHM_KEY, SHM_SIZE, IPC_CREAT | 0664);
  if(shmid < 0){
    perror("shmget");
    exit(-1);
  }
  printf("SHM_RND:%d\n", SHM_RND);
  printf("SHM_RDONLY:%d\n", SHM_RDONLY);
  struct shmid_ds ctl;
  shmctl(shmid, IPC_STAT, &ctl);
  printf("shmsize:%ld\n", ctl.shm_segsz);
  void* shmaddr = shmat(shmid, NULL, 0);
  if(shmaddr == NULL){ 
    perror("shmat");
    exit(-1);
  }
  int i = COUNT;
  while(i){
    printf("client say:%s", (char*)shmaddr);
    sleep(1);
    --i;
  }
  if(shmdt(shmaddr) < 0){
    perror("shmdt");
    exit(-1);
  }
  if(shmctl(shmid, IPC_RMID, NULL) < 0){
    perror("shmctl");
    exit(-1);
  }
  return 0;
}
